package com.clas.liu.myapplication.presenter.impl;

import android.os.Handler;
import android.os.Message;

import com.clas.liu.myapplication.model.entity.banner.BannerRoot;
import com.clas.liu.myapplication.model.entity.homeHeads.HomeHeadsRoot;
import com.clas.liu.myapplication.model.event.HomeFragmentEvent;
import com.clas.liu.myapplication.model.impl.BaseFragmentModel;
import com.clas.liu.myapplication.model.impl.HomeFragmentModel;
import com.clas.liu.myapplication.presenter.i.IHomeFragmentPresenter;
import com.clas.liu.myapplication.view.fragment.impl.BaseFragment;
import com.clas.liu.myapplication.view.fragment.impl.HomeFragment;

import java.lang.ref.WeakReference;

/**
 * Created by liu on 2019/2/28.
 */

public class HomeFragmentPresenter extends BaseFragmentPresenter<HomeFragmentModel, HomeFragment, HomeFragmentEvent>
        implements IHomeFragmentPresenter {

    private MainHandler handler;

    public HomeFragmentPresenter(HomeFragment view) {
        super(view);
        handler = new MainHandler(this);
    }

    @Override
    public void getBanner() {
        mModel.getBanner();
    }

    @Override
    public void getHeads() {
        mModel.getHeads();
    }

    @Override
    public void getNoticeCount() {
        mModel.getNoticeCount();
    }

    @Override
    protected HomeFragmentModel getModel(BaseFragmentModel.OnEventReceiveListener listener) {
        return new HomeFragmentModel(this);
    }

    @Override
    public void eventReceiveOnUiThread(Message msg) {
        switch (msg.what) {
            case HomeFragmentEvent.GET_BANNER_OK:
                BannerRoot root = ((HomeFragmentEvent) msg.obj).getBannerRoot();
                mView.bannerReturn(root.getCode(), root.getData());
                break;
            case HomeFragmentEvent.GET_BANNER_FAIL:
                mView.FailBecauseNetReturn(msg.what);
                break;
            case HomeFragmentEvent.GET_HEADS_OK:
                HomeHeadsRoot headsRoot = ((HomeFragmentEvent) msg.obj).getHeadsRoot();
                mView.headsReturn(headsRoot.getCode(), headsRoot.getData().getHeads());
                break;
            case HomeFragmentEvent.GET_HEADS_FAIL:
                mView.FailBecauseNetReturn(msg.what);
                break;
            case HomeFragmentEvent.GET_NOTICE_COUNT_FAIL:
                mView.FailBecauseNetReturn(msg.what);
                break;
            case HomeFragmentEvent.GET_NOTICE_COUNT_OK:
                mView.countReturn(((HomeFragmentEvent)msg.obj).getNoticeCount());
                break;
        }
    }

    @Override
    public void eventReceive(HomeFragmentEvent event, int what) {
        Message message = handler.obtainMessage();
        message.what = event.getWhat();
        message.obj = event;
        if (handler != null) {
            handler.sendMessage(message);
        }
    }

    /**
     * 用于获取主线程的handler
     * 这个内部类必须是静态的,不能让这个类持有外部类的引用,否则可能会造成内存泄漏
     * 使用弱引用来关联外部类,当只有弱应用存在时，变量会被回收，所以,请注意空指针!!!
     *
     * @see #eventReceiveOnUiThread(Message msg) 调用的这个方法可能会抛出NullPointerException,在这里处理掉,不要让没有
     * 必要的异常使程序崩溃
     */
    private static class MainHandler extends Handler {
        private WeakReference<HomeFragmentPresenter> presenter;     //弱引用

        MainHandler(HomeFragmentPresenter presenter) {
            this.presenter = new WeakReference<>(presenter);
        }

        @Override
        public void handleMessage(Message msg) {
            if (presenter.get() != null) {
                presenter.get().eventReceiveOnUiThread(msg);      //接收到事件后的操作,可能会抛出NullPointerException
            }
            super.handleMessage(msg);
        }
    }
}
